package com.honey.mamabike.security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by setsun on 2017/10/10.
 * 加密算法
 */
public class AESUtil {
    private static String PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3ISQsoVifuZHPyya1Cy61 snTR2h14EeRDnq3iM4Sq1ppdtT3sz6g+LcjW1ZTFn6t8OU1Epl2GTPlC+3qR Y/aytkMYsmIXdF7yHr9B1CgzYCaU73XM2Lb4A+ebLxhZQ1/BP48yIuCPJ6rG E74hpUQjp9H64SeMCblDcA/wmJBEqwIDAQAB";

    private static final String KEY_ALGORITHM ="AES";
    private static final String KEY_ALGORITHM_MODE ="AES/CBC/PKCS5Padding";

    /**
     *  AES对称加密
     * @param data
     * @param key 16位
     * @return
     */
    public static String encrypt(String data,String key){
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"),KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_MODE);
            cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte[] encypted = cipher.doFinal(data.getBytes("UTF-8"));
            return Base64Util.encode(encypted); //加密
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  AES解密
     * @param data
     * @param key
     * @return
     */
    public static String decrypt(String data,String key){
        try {
            SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"),KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_MODE);
            cipher.init(Cipher.DECRYPT_MODE,spec,new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte[] originBytes = Base64Util.decode(data);   //解密
            byte[] result = cipher.doFinal(originBytes);
            return new String(result,"UTF-8");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
        //AES对称加密数据
        String key = "123456789sdfgiul";
        String dataToEn ="老李来了...";
       /* String enResult = encrypt(dataToEn,key);
        System.out.println(enResult);


        byte[] enResult1 = RSAUtil.encryptByPublicKey(dataToEn.getBytes("UTF-8"),PUBLIC_KEY);
        System.out.println(enResult1+"11111");

        byte[] deResult = RSAUtil.decryptByPrivateKey(enResult1);
        System.out.println(new String(deResult, "UTF-8"));*/

        //RSA 加密AES密钥
    /*   byte[] enKey = RSAUtil.encryptByPublicKey(key.getBytes("UTF-8"),PUBLIC_KEY);
        System.out.println(new String(enKey,"UTF-8"));
        String baseKey = Base64Util.encode(enKey);*/
        //服务端RSA解密AES的key
        /*byte[] de = Base64Util.decode(baseKey); //先解密key
        byte[] deKeyReslt = RSAUtil.decryptByPrivateKey(de);
        System.out.println(new String(deKeyReslt,"UTF-8"));*/

//       String deResult1 = decrypt(enResult, key);
//        System.out.println(deResult1);




    }


}
